'
\ufeffin java 7.20 (Total Sales) \ufeffUse a two-dimensional array to solve the following problem: A company has four salespeople (1 \ufeffto 4) \ufeffwho sell five different products (1 \ufeffto 5). \ufeffOnce a day, each salesperson passes in a slip for each type of product sold. Each slip contains the following: The salesperson number The product number The total dollar value of that product sold that day Thus, each salesperson passes in between 0 \ufeffand 5 \ufeffsales slips per day. Assume that the information from all the slips for last month is available. Write an application that will read all this information for last month’s sales and summarize the total sales by salesperson and by product. All totals should be stored in the two-dimensional array sales. After processing all the information for last month, display the results in tabular format, with each column representing a salesperson and each row representing a particular product. Cross-total each row to get the total sales of each product for last month. Cross-total each column to get the total sales by salesperson for last month. Your output should include these cross-totals to the right of the totaled rows and to the bottom of the totaled columns. 7.21 (Turtle Graphics) \ufeffThe Logo language made the concept of turtle graphics famous. Imagine a mechanical turtle that walks around the room under the control of a Java application. The turtle holds a pen in one of two positions, up or down. While the pen is down, the turtle traces out shapes as it moves, and while the pen is up, \ufeffthe turtle moves about freely without writing anything. In this problem, you’ll simulate the operation of the turtle and create a computerized sketchpad. Use a 20-by-20 \ufeffarray floor that’s initialized to zeros. Read commands from an array that contains them. Keep track of the current position of the turtle at all times and whether the pen is currently up or down. Assume that the turtle always starts at position (0, 0) \ufeffof the floor with its pen up. \ufeffThe set of turtle commands your application must process are shown in Fig. 7.29. \ufeffFig. 7.29 \ufeffCommand Meaning 1 \ufeffPen up 2 \ufeffPen down 3 \ufeffTurn right 4 \ufeffTurn left 5,10 \ufeffMove forward 10 \ufeffspaces (replace 10 \ufefffor a different number of spaces) 6 \ufeffDisplay the 20-by-20 \ufeffarray 9 \ufeffEnd of data (sentinel) \ufeffTurtle graphics commands. Suppose that the turtle is somewhere near the center of the floor. The following “program” \ufeffwould draw and display a 12-by-12 \ufeffsquare, leaving the pen in the up position: 2 5,12 3 5,12 3 5,12 3 5,12 1 6 9 \ufeffAs the turtle moves with the pen down, set the appropriate elements of array floor to 1s. \ufeffWhen the 6 \ufeffcommand (display the array) \ufeffis given, wherever there’s a 1 \ufeffin the array, display an asterisk or any character you choose. Wherever there’s a 0, \ufeffdisplay a blank. Write an application to implement the turtle graphics capabilities discussed here. Write several turtle graphics programs to draw interesting shapes. Add other commands to increase the power of your turtle graphics language.
7.22 (Knight’s Tour) \ufeffAn interesting puzzler for chess buffs is the Knight’s Tour problem, originally proposed by the mathematician Euler. Can the knight piece move around an empty chess-board and touch each of the 64 \ufeffsquares once and only once? We study this intriguing problem in depth here. The knight makes only L-shaped moves (two spaces in one direction and one space in a perpendicular direction). \ufeffThus, as shown in Fig. 7.30, \ufefffrom a square near the middle of an empty chessboard, the knight (labeled K) \ufeffcan make eight different moves (numbered 0 \ufeffthrough 7).
\ufeffFig. 7.30 \ufeffThe eight possible moves of the knight. An 8 \ufeffby 8 \ufeffarray where the rows and columns are numbered from 0 \ufeffto 7 \ufeffeach. The knight is positioned on row 3, \ufeffcolumn 4. \ufeffThe eight possible moves for the knight are: possibility 0, \ufeffrow 2, \ufeffcolumn 6; possibility 1, \ufeffrow 1, \ufeffcolumn 5; possibility 2, \ufeffrow 1, \ufeffcolumn 3; possibility 3, \ufeffrow 2, \ufeffcolumn 2; possibility 4, \ufeffrow 4, \ufeffcolumn 2; possibility 5, \ufeffrow 5, \ufeffcolumn 3; possibility 6, \ufeffrow 5, \ufeffcolumn 5; possibility 7, \ufeffrow 4, \ufeffcolumn 6. \ufeffDraw an eight-by-eight chessboard on a sheet of paper, and attempt a Knight’s Tour by hand. Put a 1 \ufeffin the starting square, a 2 \ufeffin the second square, a 3 \ufeffin the third, and so on. \ufeffBefore starting the tour, estimate how far you think you’ll get, remembering that a full tour consists of 64 \ufeffmoves. How far did you get? Was this close to your estimate? Now let’s develop an application that will move the knight around a chessboard. The board is represented by an eight-by-eight two-dimensional array board. Each square is initialized to zero. We describe each of the eight possible moves in terms of its horizontal and vertical components. For example, a move of type 0, \ufeffas shown in Fig. 7.30, \ufeffconsists of moving two squares horizontally to the right and one square vertically upward. A move of type 2 \ufeffconsists of moving one square horizontally to the left and two squares vertically upward. Horizontal moves to the left and vertical move